package com.zc.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zc.dao.impl.UserDaoImpl;
import com.zc.model.User;

public class UserAction extends ActionSupport implements ModelDriven<User> {

	private static final long serialVersionUID = 1L;
	private User user = new User();

//	@Override
//	public String execute() throws Exception {
//		return SUCCESS;
//	}

	/**
	 * 用户登录，这个时候，根据用户名和密码去数据库中查找对应的用户，看是否存在，如果存在，则返回首页，同时在顶部显示用户信息，如果失败，则跳到错误页面user/error.jsp
	 * 
	 * @return
	 */
	public String login() {
		System.out.println(user.toString());
		UserDaoImpl dao = new UserDaoImpl();
		User u = dao.getUser(user);
		if (u != null) {
			ActionContext context = ActionContext.getContext();

			Map<String, Object> session = context.getSession();
			session.put("user", u);
			System.out.println("找到user");
			return SUCCESS;
		} else {
			System.out.println("未找到user");
			return ERROR;
		}

		// System.out.println(user.getUsername());
		// System.out.println(user.getPassword());
		// return SUCCESS;
	}

	public String logout() {
		System.out.println("执行注销功能");
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		System.out.println(session.values());
//		session.remove("user", user);
		session.put("user", null);
//		session.clear();
//		session.put("user", null);
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public User getModel() {
		// System.out.println(user.toString());
		return this.user;
	}
	
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		super.validate();
	}
}
